<template>
	<view>
		<view class="search-box">
			<uni-search-bar placeholder="自定义背景色" bgColor="#EEEEEE" @input="input" radius=18 cancelButton='none'/>
			</uni-search-bar>
		</view>
		<!-- 搜索结果 -->
		<view class="result-list"  v-if="searchResult.length !==0 ">
			<view class="result-item"  v-for="(item,i) in searchResult" :key="i" @click="gotoDetail(item)">
				<view class="good-name">{{item.goods_name}}</view>
				<uni-icons type="right" size="18"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash-filled" size="18" ></uni-icons>
			</view>
			<!-- 搜索历史 -->
			<view class="history-data">
				<uni-tag :text="item" v-for="(item,i) in histories" :key="i" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				time:null,
				keyword:'',
				searchResult:[],
				historydata:[]
			};
		},
		onLoad() {
			this.historydata=JSON.parse(uni.getStorageSync('keyword')||'[]')
		},
		methods:{
			input(e){
				clearTimeout(this.time)
				/* 延时输出数据，防抖动 */
				this.time=setTimeout(()=>{
					this.keyword=e,
					this.getSearchList()
				},500)
				
			},
			async getSearchList(){
				if(this.keyword.length===0){
					this.searchResult=[]
					return
				}
				const {data:res}=await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.keyword})
				if(res.meta.status!==200) return uni.$showMsg()
				this.searchResult=res.message
				this.saveHistory()
			},
			gotoDetail(item){
				uni.navigateTo({
					url:'/subpkg/good_list/good_list?id='+item.goods_id
				})
			},
			saveHistory(){
				/* this.historydata.push(this.keyword)
				this.historydata=unshift */
				const set=new Set(this.historydata)
				set.delete(this.keyword)
				set.add(this.keyword)
				this.historydata=Array.from(set)
				/* 对搜索历史进行持久化存储 */
				uni.setStorageSync('keyword',JSON.stringify(this.historydata))
			},
			gotoGoodsList(item){
				uni.navigateTo({
					url:'/subpkg/good_list/good_list?query='+item
				})
			}
		},
		computed:{
			histories(){
				return[...this.historydata].reverse()
			}
		}
	}
</script>

<style lang="scss">
	
	.search-box{
		position: sticky;
		top: 0;
		z-index: 999;
	}
	.result-list{
		.result-item{
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 12px;
			padding: 13px 0;
			margin-left: 5px;
			border-bottom: 1px solid #efefef;
			.good-name{
				white-space: nowrap;
				overflow: hidden;
				text-overflow:ellipsis;
			}
		}
	}
	.history-box{
		padding: 0 5px;
		.history-title{
			display: flex;
			justify-content: space-between;
			height: 40px;
			align-items: center;
			font-size: 13px;
			border-bottom:1px solid #efefef;
		}
		.history-data{
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
		}
		.uni-tag{
			display: block;
			margin-right: 5px;
			margin-top: 5px;
			font-size: 16px;
		}
	}
</style>
